package com.xiaoran.springbt02web.config;

import com.xiaoran.springbt02web.bean.Pet;
import com.xiaoran.springbt02web.conver.XiaoranMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;
import sun.plugin2.message.helper.URLHelper;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@org.springframework.context.annotation.Configuration(proxyBeanMethods = false)
public class Configuration implements WebMvcConfigurer {
    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
        HiddenHttpMethodFilter hiddenHttpMethodFilter=new HiddenHttpMethodFilter();
        hiddenHttpMethodFilter.setMethodParam("_me");
         return hiddenHttpMethodFilter;
        }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper  urlPathHelper=new UrlPathHelper();
        /**
        * Set if ";" (semicolon) content should be stripped from the request URI.
        * <p>Default is "true".
         */
        //默认是为ture，移除分号后面的内容，我们需要改为false，不移除分号后面的内容
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }

    //自定义参数解析器
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new Converter<String,Pet>() {
            @Override
            public Pet convert(String source) {
                if (!source.equals("")){
                    String[] list=source.split(",");
                    Pet pet=new Pet();
                    pet.setName(list[0]);
                    pet.setAge(list[1]);
                    return pet;
                }
                return null;
            }
        });
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new XiaoranMessageConverter());
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        Map<String ,MediaType> map=new HashMap<>();
        map.put("json",MediaType.APPLICATION_JSON);
        map.put("json",MediaType.APPLICATION_ATOM_XML);
        map.put("gg",MediaType.parseMediaType("application/x-xiaoran"));
        //Map<String, MediaType> mediaTypes
        ParameterContentNegotiationStrategy Parameterstrategy=new ParameterContentNegotiationStrategy(map);
        HeaderContentNegotiationStrategy Headerstrategy=new HeaderContentNegotiationStrategy();
        configurer.strategies(Arrays.asList(Parameterstrategy,Headerstrategy));
    }
}
